package com.pur.zsms.unit.web;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.service.IUserService;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.pmsnmanage.user.web.UserControllor;
import com.pur.zsms.supplier.po.ShopSupplierTypePo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.ShopSupplierVo;
import com.pur.zsms.supplier.vo.SupplierVo;
import com.pur.zsms.unit.service.IUnitService;
import com.pur.zsms.unit.vo.UnitCommentVo;
import com.pur.zsms.unit.vo.UnitMenuVo;
import com.pur.zsms.unit.vo.UnitVo;

/**
 * 单位评论模块
 * 
 * @author zhou
 * 
 */
@Controller
@RequestMapping(value = "/unit")
public class UnitController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(UnitController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "unitService")
	private IUnitService unitService;

	@RequestMapping(value = "/listUnitComment.do")
	@ResponseBody
	public String listUnitComment(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, UnitCommentVo unitCommentVo)
	        throws GenericException, IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = unitService.listUnitComment(pageController, unitCommentVo);
		List items = pageList.getItems();
		List<UnitCommentVo> tUnitCommentVoList = new ArrayList<UnitCommentVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				UnitCommentVo f = new UnitCommentVo();
				f.setCid((Integer) item[0]);
				f.setUnitId((Integer) item[1]);
				f.setUnitName((String) item[2]);
				f.setCommentState((Integer) item[3]);
				f.setCommentText((String) item[4]);
				f.setCommentDate((String) item[5]);
				tUnitCommentVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", tUnitCommentVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/listUnitMenu.do")
	@ResponseBody
	public String listUnitMenu(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, UnitMenuVo unitMenuVo) throws GenericException,
	        IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = unitService.listUnitMenu(pageController, unitMenuVo);
		List items = pageList.getItems();
		List<UnitMenuVo> tUnitMenuVoList = new ArrayList<UnitMenuVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				UnitMenuVo f = new UnitMenuVo();
				f.setMenuId((Integer) item[0]);
				f.setUnitId((Integer) item[1]);
				f.setUnitName((String) item[2]);
				f.setMenuDate((String) item[3]);
				f.setMenuText((String) item[4]);
				tUnitMenuVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", tUnitMenuVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/listUnit.do")
	@ResponseBody
	public String listUnit(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows, UnitVo unitVo) throws GenericException,
	        IllegalAccessException, InvocationTargetException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = unitService.listUnit(pageController, unitVo);
		List items = pageList.getItems();
		List<UnitVo> tUnitVoList = new ArrayList<UnitVo>();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				UnitVo f = new UnitVo();
				f.setUnitId((Integer) item[0]);
				f.setUnitName((String) item[1]);
				f.setUnitCode((String) item[2]);

				tUnitVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", tUnitVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preSaveUnitMenu.do", method = { RequestMethod.GET })
	public ModelAndView preSaveUnitMenu() {

		Map map = new HashMap();
		return new ModelAndView("/unit/addUnitMenu", map);
	}

	@RequestMapping(value = "/pulldownUnitId.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownUnitId(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = unitService.getAllUnitPoJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	@RequestMapping(value = "/saveUnitMenu.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveUnitMenu(UnitMenuVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			/*
			 * UserPo userInfo = (UserPo) session.getAttribute("user"); int
			 * supId = supplierService.getSupIdByUserId(userInfo.getUserId());
			 * c.setSupId(supId);
			 */

			boolean flag = unitService.addUnitMenu(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", unitService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/preUpdateUnitMenu.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateUnitMenu(UnitMenuVo c) {
		UnitMenuVo tUnitMenuVo = null;
		try {
			tUnitMenuVo = unitService.getUnitMenuPo(c);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("unitMenuVo", tUnitMenuVo);
		return new ModelAndView("/unit/updateUnitMenu", map);
		// return new ModelAndView("/unit/addUnitMenu.jsp", map);

	}

	@RequestMapping(value = "/updateUnitMenu.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateUnitMenu(UnitMenuVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {

			boolean flag = unitService.updateUnitMenu(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", unitService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}
}
